package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp05Pricing;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class PricingBySupply implements CustomExpressionFactory {

    /**
     * Busca el pricing relacionado a un Supply, segun la fecha actual del sistema
     */
    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;

        Teesp05Pricing supply = (Teesp05Pricing) parameters[0];
        
        Timestamp actualDate = new Timestamp(System
                .currentTimeMillis());

        
        ExpressionBuilder builderReport = new ExpressionBuilder();        
        Expression subQ = null;
        ReportQuery report = new ReportQuery();
        report = new ReportQuery(Teesp05Pricing.class, builderReport);
        report.addMaximum("eesp05PriceEffectiveDateY", builderReport.get("eesp05PriceEffectiveDateY"));
        subQ =builderReport.get("teesp04Supply").equal(supply.getTeesp04Supply());
        subQ = subQ.and(builder.get("eesp05PriceEffectiveDateY").lessThanEqual(actualDate));
        report.setSelectionCriteria(subQ);
        
        
        
        exp = builder.get("teesp04Supply").equal(supply.getTeesp04Supply());
        exp = exp.and(builder.get("eesp05PriceEffectiveDateY").equal(exp.subQuery(report)));
        
        return exp;
    }

}
